package com.weishu.installment.controller;

import cn.hutool.core.util.RandomUtil;
import com.alibaba.nacos.shaded.io.grpc.netty.shaded.io.netty.util.internal.StringUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.weishu.installment.domain.TJrRentInstallment;
import com.weishu.installment.domain.TJrSfLoan;
import com.weishu.installment.domain.shenhe.FinanceApplyStateEnum;
import com.weishu.installment.domain.shenhe.JrContractSelectRequestDto;
import com.weishu.installment.domain.shenhe.JrRentInstallmentResponseDto;
import com.weishu.installment.domain.shenhe.JrRentInstallmentSearchPage;
import com.weishu.installment.service.TJrRentInstallmentService;
import com.weishu.installment.service.TJrSfLoanService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("installment")
public class InstallmentController {
    @Resource
    TJrRentInstallmentService service;
    @Resource
    RedisTemplate redisTemplate;

    @PostMapping("list")
    @RequiresPermissions("installment:installment:list")
    public Map list(@RequestBody TJrRentInstallment ment){
        System.err.println(ment.getPageNum());
        System.err.println(ment.getPageSize());
        Page page = new Page(ment.getPageNum(),ment.getPageSize());
        Page page1 = service.page(page);
        System.out.println(page1.getRecords());
        Map map = new HashMap();
        map.put("total",page1.getTotal());
        map.put("list",page1.getRecords());
        return map;
    }



    @PostMapping("saverentinstallment")
    @RequiresPermissions("installment:installment:saverentinstallment")
    public Map saverentinstallment(@RequestBody TJrRentInstallment ment){
        System.err.println(ment);
        Map map = new HashMap();
        ment.setRentInstallmentOrderNo("NO"+RandomUtil.randomString(6));
        ment.setOutOrderNo("NO"+RandomUtil.randomString(6));
        ment.setCreateTime(new Date());
        boolean b = service.save(ment);
        if (b){
            map.put("code",200);
            System.out.println(".....................");
            return map;
        }
        map.put("code",2002);
        return map;
    }

    @RequiresPermissions("installment:installment:searchRentInstall")
    @PostMapping("searchRentInstall")
    public Map searchRentInstall(@RequestBody JrRentInstallmentSearchPage jrRentInstallmentSearchPage)  {
        Map map = new HashMap();
        PageHelper.startPage(jrRentInstallmentSearchPage.getPageNum(), jrRentInstallmentSearchPage.getPageSize());
        List<JrRentInstallmentResponseDto> list = service.searchLoans(jrRentInstallmentSearchPage);
        PageInfo pageInfo = new PageInfo(list);
        map.put("list",pageInfo.getList());
        System.err.println(pageInfo.getList());
        map.put("total",pageInfo.getTotal());
        map.put("pageSize",pageInfo.getPageSize());
        map.put("pageNum",pageInfo.getPageNum());
        return map;
    }

    @Resource
    TJrSfLoanService jrSfLoanService;

    @RequiresPermissions("installment:installment:firstSH")
    @GetMapping("firstSH")
    public Map<String,Object> firstSH(Integer id,String applyOrderNo) {
        Map<String ,Object> map = new HashMap<>();
        if (StringUtil.isNullOrEmpty(applyOrderNo)) {
            map.put("flag",false);
            return map;
        }
        String substring = applyOrderNo.substring(0, 2);
        if (substring.equals("SF")){
            TJrSfLoan tJrSfLoan = jrSfLoanService.getById(id);
            tJrSfLoan.setApplyState(2);
            tJrSfLoan.setUpdateTime(new Date());
            boolean flag = jrSfLoanService.saveOrUpdate(tJrSfLoan);
            map.put("flag",flag);
            return map;
        } else if (substring.equals("FZ")){
            TJrRentInstallment tJrRentInstallment = service.getById(id);
            tJrRentInstallment.setApplyState(2);
            tJrRentInstallment.setUpdateTime(new Date());
            boolean flag = service.saveOrUpdate(tJrRentInstallment);
            map.put("flag",flag);
            return map;
        } else {
            //租约保理初审核
            map.put("flag",false);
            return map;
        }
    }


    @RequiresPermissions("installment:installment:secondSH")
    @GetMapping("secondSH")
    public Map<String,Object> secondSH(Integer id,String applyOrderNo) {
        Map<String ,Object> map = new HashMap<>();
        if (StringUtil.isNullOrEmpty(applyOrderNo)) {
            map.put("flag",false);
            return map;
        }
        String substring = applyOrderNo.substring(0, 2);
        if (substring.equals("SF")){
            TJrSfLoan tJrSfLoan = jrSfLoanService.getById(id);
            tJrSfLoan.setApplyState(4);
            tJrSfLoan.setUpdateTime(new Date());
            boolean flag = jrSfLoanService.saveOrUpdate(tJrSfLoan);
            map.put("flag",flag);
            return map;
        } else if (substring.equals("FZ")){
            TJrRentInstallment installment = service.getById(id);
            installment.setApplyState(4);
            installment.setUpdateTime(new Date());
            boolean flag = service.saveOrUpdate(installment);
            map.put("flag",flag);
            return map;
        } else {
            //租约保理初审核
            map.put("flag",false);
            return map;
        }
    }


    @RequiresPermissions("installment:installment:firstnoSH")
    @GetMapping("firstnoSH")
    public Map<String,Object> firstnoSH(Integer id,String applyOrderNo) {
        Map<String ,Object> map = new HashMap<>();
        if (StringUtil.isNullOrEmpty(applyOrderNo)) {
            map.put("flag",false);
            return map;
        }
        String substring = applyOrderNo.substring(0, 2);
        if (substring.equals("SF")){
            TJrSfLoan sfLoan = jrSfLoanService.getById(id);
            sfLoan.setApplyState(3);
            sfLoan.setUpdateTime(new Date());
            boolean flag = jrSfLoanService.saveOrUpdate(sfLoan);
            map.put("flag",flag);
            return map;
        } else if (substring.equals("FZ")){
            TJrRentInstallment rentInstallment = service.getById(id);
            rentInstallment.setApplyState(3);
            rentInstallment.setUpdateTime(new Date());
            boolean flag = service.saveOrUpdate(rentInstallment);
            map.put("flag",flag);
            return map;
        } else {
            //租约保理初审核
            map.put("flag",false);
            return map;
        }
    }


    @RequiresPermissions("installment:installment:secondnoSH")
    @GetMapping("secondnoSH")
    public Map<String,Object> secondnoSH(Integer id,String applyOrderNo) {
        Map<String ,Object> map = new HashMap<>();
        if (StringUtil.isNullOrEmpty(applyOrderNo)) {
            map.put("flag",false);
            return map;
        }
        String substring = applyOrderNo.substring(0, 2);
        if (substring.equals("SF")){
            TJrSfLoan sfLoan = jrSfLoanService.getById(id);
            sfLoan.setApplyState(5);
            sfLoan.setUpdateTime(new Date());
            boolean flag = jrSfLoanService.saveOrUpdate(sfLoan);
            map.put("flag",flag);
            return map;
        } else if (substring.equals("FZ")){
            TJrRentInstallment rentInstallment = service.getById(id);
            rentInstallment.setApplyState(5);
            rentInstallment.setUpdateTime(new Date());
            boolean flag = service.saveOrUpdate(rentInstallment);
            map.put("flag",flag);
            return map;
        } else {
            //租约保理初审核
            map.put("flag",false);
            return map;
        }
    }

















//    @RequiresPermissions("installment:installment:edit")
//    @RequestMapping("/auditPassRentReview")
//    @ResponseBody
//    public Map auditPassRecieveview(Integer applyState,
//                                  String cfContractId, Long id, String auditMemo) throws ParseException {
//        JrContractSelectRequestDto contractSelect=new JrContractSelectRequestDto();
//        //申请状态，1:申请中，2：初审通过(待复审)，3：初审不通过，4：复审通过，5：复审不通过
//        if(applyState == FinanceApplyStateEnum.CSPASS.getIndex()||applyState == FinanceApplyStateEnum.FSPASS.getIndex()){
//            //审核通过
//            contractSelect.setId(id);
//            contractSelect.setApplyState(applyState);
//            contractSelect.setCfContractId(Long.parseLong(cfContractId));
//            service.updateAuditPassRenting(contractSelect);
//        }else{
//            // 审核不通过
//            contractSelect.setApplyState(applyState);
//            contractSelect.setId(id);
//            contractSelect.setAuditMemo(auditMemo);
//            contractSelect.setCfContractId(Long.parseLong(cfContractId));
//            service.updateAuditNoPassRenting(contractSelect);
//        }
//        return null;
//    }









    /**
     * 同意审核
     */
    @RequiresPermissions("installment:installment:edit")
    @RequestMapping("updateStatus")
    public Boolean updateStatus(@RequestBody TJrRentInstallment ment){
        if (ment.getApplyState()==2){
            ment.setApplyState(4);
            boolean b = service.saveOrUpdate(ment);
            return b;
        }
        ment.setApplyState(2);
        boolean b = service.saveOrUpdate(ment);
        return b;
    }

    /**
     * 未同意审核
     */
    @RequiresPermissions("installment:installment:edit")
    @RequestMapping("updateStatus2")
    public Boolean updateStatus2(@RequestBody TJrRentInstallment ment){
        if (ment.getApplyState()==2){
            ment.setApplyState(5);
            boolean b = service.saveOrUpdate(ment);
            return b;
        }
        ment.setApplyState(3);
        boolean b = service.saveOrUpdate(ment);
        return b;
    }
}
